﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cow.Module;
using System.IO;
using System.Text.RegularExpressions;

namespace Cow
{
    /// <summary>
    /// 通过拼接字符串的方式导出
    /// </summary>
    public abstract class ExportFromStringBuilder : IExportHandler
    {
        public virtual string PrefixName
        {
            get { return String.Empty; }
        }

        public virtual string SuffixName
        {
            get { return String.Empty; }
        }
        protected abstract String TemplatePath { get; }
        protected abstract List<String> GetExportContent(Design design);
        public void Export(Design design)
        {
            List<String> valueList = GetExportContent(design);
            String content = String.Empty;
            String templateContent = "{0}";
            if (!String.IsNullOrWhiteSpace(TemplatePath))
            {
                using (StreamReader streamReader = new StreamReader(TemplatePath, Encoding.Default))
                {
                    templateContent = streamReader.ReadToEnd();
                }
                var regex = new Regex(@"\{(?!\d|\{)");
                templateContent = regex.Replace(templateContent, "{{");
                regex = new Regex(@"(?<!\d|\})\}");
                templateContent = regex.Replace(templateContent, "}}");
            }
            content = String.Format(templateContent, valueList.ToArray());
            using (StreamWriter sw = new StreamWriter(design.OutputPath))
            {
                sw.Write(content);
            }
        }
    }
}